package com.liuyc.lib.common.mvvm.vm

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * BaseViewModel
 *
 * 刘隽
 */
open class BaseViewModel : ViewModel(){

    /**
     * 在viewModel 均使用这个方法作为顶层父协程
     * 该步骤还未进行 线程池设置
     */
    fun supperCoroutinesScope(block: suspend () -> Unit ){
        viewModelScope.launch {
            block()
        }
    }


    /**
     * 执行IO操作的协程
     */
    fun ioCoroutinesScope(block: suspend () -> Unit ){
        viewModelScope.launch {
            doIoCoroutinesScope(block)
        }
    }

    protected suspend fun doIoCoroutinesScope(block: suspend () -> Unit ){
        withContext(Dispatchers.IO){
            block()
        }
    }

    fun test(){
        ioCoroutinesScope {

        }
    }

}